//
//  HistoryViewController.swift
//  Loop
//
//  Created by thomas on 2020/9/23.
//

import UIKit

class HistoryViewController: UITableViewController {

    @IBOutlet var titleView: UIView!

    private var list: [GithubRequest]?

    private lazy var newRequestFlag: UIBarButtonItem = {
        let barButtonItem = UIBarButtonItem(image: UIImage(named: "flag"),
                                            style: .plain,
                                            target: nil,
                                            action: nil)
        return barButtonItem
    }()

    // MARK: - LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        self.loadHistory()
        self.navigationItem.rightBarButtonItem = nil
        self.registerObserver()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }

    // MARK: - UI
    private func configUI() {

        navigationItem.titleView = titleView

        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self,
                                 action: #selector(pullToRefresh),
                                 for: .valueChanged)
        self.refreshControl = refreshControl

        tableView.tableFooterView = UIView()
    }

    @objc private func pullToRefresh() {
        loadHistory()
        self.refreshControl?.endRefreshing()
        self.navigationItem.rightBarButtonItem = nil
    }

    // MARK: - Observer
    private func registerObserver() {

        NotificationCenter.default.addObserver(
            forName: .StoreServiceDidInsertRequestNotification,
            object: nil,
            queue: OperationQueue.main
        ) { notify in
            self.navigationItem.rightBarButtonItem = self.newRequestFlag
        }
    }

    // MARK: - Data
    private func loadHistory() {
        self.list = StoreService.shared.getAllRequestHistory()
        self.tableView.reloadData()
    }
}

// MARK: - UITableViewDataSource
extension HistoryViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list?.count ?? 0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "UITableViewCell")
            cell!.selectionStyle = .none
        }

        guard let request = list?[indexPath.row] else {
            return cell!
        }

        if indexPath.row == 0 {
            cell?.imageView?.image = UIImage(named: "flag")
            cell?.textLabel?.textColor = .red
        } else {
            cell?.imageView?.image = nil
            cell?.textLabel?.textColor = .black
        }

        cell?.textLabel?.text = request.time?.description

        return cell!
    }
}
